package com.gpframework.module.controller.wages;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.wages.domain.GzFleetMonthlyRecord;
import com.gpframework.module.wages.domain.req.GzFleetMonthlyRecordAdd;
import com.gpframework.module.wages.domain.req.GzFleetMonthlyRecordReq;
import com.gpframework.module.wages.domain.req.GzFleetMonthlyRecordUpdate;
import com.gpframework.module.wages.domain.vo.GzFleetMonthlyRecordVo;
import com.gpframework.module.wages.service.IGzFleetMonthlyRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GzFleetMonthlyRecordController
 * @Author gpframework
 * @Date 2024-01-19
 * @Version 1.0.0
 * @Description 车队月度Controller
 */
@Validated
@Api(tags = "车队月度")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/wages/fleetmonthlyrecord" )
public class GzFleetMonthlyRecordController extends BaseController {

    private final IGzFleetMonthlyRecordService iGzFleetMonthlyRecordService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-19
  * @Param gzFleetMonthlyRecord
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('wages:fleetmonthlyrecord:list')")
    @GetMapping("/list")
    public TableDataInfo<GzFleetMonthlyRecordVo> list(GzFleetMonthlyRecordReq gzFleetMonthlyRecordReq)
    {
        startPage();
        QueryWrapper<GzFleetMonthlyRecord> lqw = new QueryWrapper<GzFleetMonthlyRecord>();
        gzFleetMonthlyRecordReq.generatorQuery(lqw,true);
        List<GzFleetMonthlyRecordVo> list = iGzFleetMonthlyRecordService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzFleetMonthlyRecord
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('wages:fleetmonthlyrecord:add')" )
    @Log(title = "添加车队月度" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GzFleetMonthlyRecordAdd gzFleetMonthlyRecordAdd) {

        iGzFleetMonthlyRecordService.remove(new QueryWrapper<GzFleetMonthlyRecord>().eq("month", gzFleetMonthlyRecordAdd.getMonth()));


        GzFleetMonthlyRecord gzFleetMonthlyRecord = Convert.convert(new TypeReference<GzFleetMonthlyRecord>() {}, gzFleetMonthlyRecordAdd);


        //金额=出车车次*单价
        gzFleetMonthlyRecord.setAmountOfMoney(
                gzFleetMonthlyRecordAdd.getUnitPrice().multiply(new
                        BigDecimal(gzFleetMonthlyRecordAdd.getTrainNumber()))
        );
        //实发合计=金额+扣减餐费+其他补贴
        gzFleetMonthlyRecord.setTotalPaidIn(
                gzFleetMonthlyRecord.getAmountOfMoney().add(gzFleetMonthlyRecord.getMealBuckle()
                        .add(gzFleetMonthlyRecord.getOtherSubsidies())
        ));
        gzFleetMonthlyRecord.setDepartmentName("车队");
        return toAjax(iGzFleetMonthlyRecordService.save(gzFleetMonthlyRecord) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzFleetMonthlyRecord
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('wages:fleetmonthlyrecord:edit')" )
    @Log(title = "修改车队月度" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GzFleetMonthlyRecordUpdate gzFleetMonthlyRecordUpdate) {
        GzFleetMonthlyRecord gzFleetMonthlyRecord = Convert.convert(new TypeReference<GzFleetMonthlyRecord>() {}, gzFleetMonthlyRecordUpdate);
        //金额=出车车次*单价
        gzFleetMonthlyRecord.setAmountOfMoney(
                gzFleetMonthlyRecordUpdate.getUnitPrice().multiply(new
                        BigDecimal(gzFleetMonthlyRecordUpdate.getTrainNumber()))
        );
        //实发合计=金额+扣减餐费+其他补贴
        gzFleetMonthlyRecord.setTotalPaidIn(
                gzFleetMonthlyRecord.getAmountOfMoney().add(gzFleetMonthlyRecord.getMealBuckle()
                        .add(gzFleetMonthlyRecord.getOtherSubsidies())
                ));
        gzFleetMonthlyRecord.setDepartmentName("车队");
        return toAjax(iGzFleetMonthlyRecordService.updateById(gzFleetMonthlyRecord) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzFleetMonthlyRecord
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('wages:fleetmonthlyrecord:export')" )
    @Log(title = "导出车队月度" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GzFleetMonthlyRecordReq gzFleetMonthlyRecordReq) {
        QueryWrapper<GzFleetMonthlyRecord> lqw = new QueryWrapper<GzFleetMonthlyRecord>();
        gzFleetMonthlyRecordReq.generatorQuery(lqw,true);
        List<GzFleetMonthlyRecordVo> list = iGzFleetMonthlyRecordService.listVo(lqw);
        ExcelUtil<GzFleetMonthlyRecordVo> util = new ExcelUtil<GzFleetMonthlyRecordVo>(GzFleetMonthlyRecordVo. class);
        return util.exportExcel(list, "fleetmonthlyrecord" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzFleetMonthlyRecord
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('wages:fleetmonthlyrecord:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GzFleetMonthlyRecordVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGzFleetMonthlyRecordService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzFleetMonthlyRecord
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('wages:fleetmonthlyrecord:remove')" )
    @Log(title = "删除车队月度" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGzFleetMonthlyRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
